# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :common_business.py
# @Time      :2025/1/26 13:45:34
# @Author    :flower
import os
import sys

from data.apis import APIs
from util.get_file import GetFile
from util.get_http import GetHttp
from util.get_log import logger
from util.get_time import GetTime


class CommonBusiness:

    # 创建项目
    def create_project(self, name='UAF' + GetTime.get_local_second(), url=APIs.CommonCreateAPI,
                       json_file='create_project.json'):
        logger.info(f'{self.create_project.__name__}业务')
        data = GetFile.read_json_file(json_file)
        data['name'] = name
        data["dirPath"] = os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0])))+os.sep+'data'+os.sep+'auto_can_del'
        response = GetHttp().do_post_request(url=url, data=data)
        return response

    # 保存项目
    def save_project(self, project_id, url=APIs.CommonSaveAPI, json_file='save_project.json'):
        logger.info(f'{self.save_project.__name__}业务')
        data = GetFile.read_json_file(json_file)
        data['projectId'] = project_id
        response = GetHttp().do_post_request(url=url, data=data)
        return response

    # 查询xx可创建的子xx列表 以根模型为例，查询可创建的元素列表
    def node_canCreateChildTypes(self, project_id, root_id, url='node/canCreateChildTypes?'):
        logger.info(f'{self.node_canCreateChildTypes.__name__}业务')
        url = url + f'projectId={project_id}&instanceId={root_id}'
        response = GetHttp().do_get_request(url=url)
        return response

    @GetTime.run_cost_time
    # 根模型下创建战略/战略动机图
    def model_create_strategy(self, project_id, root_id, url=APIs.ModelCreateAPI,
                              json_file='create_strategy.json'):
        # 战略
        data = GetFile.read_json_file(json_file)
        data['projectId'] = project_id
        data['parentId'] = root_id
        response = GetHttp().do_post_request(url=url, data=data)

        # 战略动机图
        data = GetFile.read_json_file('create_strategy_diagram.json')
        data['projectId'] = project_id
        data['parentId'] = response['data'][0]['id']
        response = GetHttp().do_post_request(url=url, data=data)
        id_ = response['data'][0]['id']

        # 查询图diagramid
        response = GetHttp().do_get_request(url=f'shape/diagram/allShape?projectId={project_id}&modelId={id_}')

        # 图拖拽能力元素
        data = GetFile.read_json_file('create_capability.json')
        data['projectId'] = project_id
        data['targetShapeId'] = response['data'][0]['diagramId']
        data['diagramId'] = data['targetShapeId']
        shape_list = []
        data2 = GetFile.read_json_file('shape_connect.json')
        for x in range(100, 500, 200):  # 4000
            for y in range(100, 500, 100):  # 4000
                data['point']['x'] = x
                data['point']['y'] = y
                response = GetHttp().do_post_request(url=APIs.ShapeSiderBarDropAPI, data=data)
                shape_id = response['data']['shapeId']
                shape_ = {"shape_id": shape_id, "x": x, "y": y}
                shape_list.append(shape_)
                if len(shape_list) >= 2:
                    data2['projectId'] = project_id
                    data2['diagramId'] = data['targetShapeId']
                    data2['sourceShapeId'] = shape_list[0]['shape_id']
                    data2['targetShapeId'] = shape_list[1]['shape_id']
                    data2['waypoint'][0]['x'] = shape_list[0]['x']
                    data2['waypoint'][0]['y'] = shape_list[0]['y']
                    data2['waypoint'][1]['x'] = shape_list[1]['x']
                    data2['waypoint'][1]['y'] = shape_list[1]['y']
                    response = GetHttp().do_post_request(url=APIs.ShapeConnectShapeApI, data=data2)
                    shape_list.pop(0)
            else:
                pass
        return response

    @GetTime.run_cost_time
    # 根模型下创建业务
    def model_create_operational(self, project_id, root_id, url=APIs.ModelCreateAPI,
                                 json_file='common/create_strategy.json'):
        # 业务
        data = GetFile.read_json_file(json_file)
        data['projectId'] = project_id
        data['parentId'] = root_id
        data['applyStereotypes'][0] = '16360188956224'
        response = GetHttp().do_post_request(url=url, data=data)

        # 业务分类图
        data = GetFile.read_json_file('common/create_strategy_diagram.json')
        data['projectId'] = project_id
        data['parentId'] = response['data'][0]['id']
        data['applyStereotypes'][0] = '16365315118208'
        response = GetHttp().do_post_request(url=url, data=data)
        id_ = response['data'][0]['id']

        # 查询图diagramid
        response = GetHttp().do_get_request(url=f'shape/diagram/allShape?projectId={project_id}&modelId={id_}')

        # 图拖拽业务执行者元素
        data = GetFile.read_json_file('common/create_capability.json')
        data['projectId'] = project_id
        data['targetShapeId'] = response['data'][0]['diagramId']
        data['diagramId'] = data['targetShapeId']
        data['sourceType'] = 'UAF::Operational::Structure::OperationalPerformer'
        data['payload']['applyStereotypes'][0] = data['sourceType']
        shape_list = []
        data2 = GetFile.read_json_file('common/shape_connect.json')
        for x in range(100, 500, 200):  # 4000
            for y in range(100, 500, 100):  # 4000
                data['point']['x'] = x
                data['point']['y'] = y
                response = GetHttp().do_post_request(url=APIs.ShapeSiderBarDropAPI, data=data)
                shape_id = response['data']['shapeId']
                shape_ = {"shape_id": shape_id, "x": x, "y": y}
                shape_list.append(shape_)
                if len(shape_list) >= 2:
                    data2['projectId'] = project_id
                    data2['diagramId'] = data['targetShapeId']
                    data2['sourceShapeId'] = shape_list[0]['shape_id']
                    data2['targetShapeId'] = shape_list[1]['shape_id']
                    data2['waypoint'][0]['x'] = shape_list[0]['x']
                    data2['waypoint'][0]['y'] = shape_list[0]['y']
                    data2['waypoint'][1]['x'] = shape_list[1]['x']
                    data2['waypoint'][1]['y'] = shape_list[1]['y']
                    response = GetHttp().do_post_request(url=APIs.ShapeConnectShapeApI, data=data2)
                    shape_list.pop(0)
            else:
                pass
        return response

    @GetTime.run_cost_time
    # 根模型下创建服务
    def model_create_services(self, project_id, root_id, url=APIs.ModelCreateAPI,
                              json_file='common/create_strategy.json'):
        # 服务
        data = GetFile.read_json_file(json_file)
        data['projectId'] = project_id
        data['parentId'] = root_id
        data['applyStereotypes'][0] = '16360210837184'
        response = GetHttp().do_post_request(url=url, data=data)

        # 服务分类图
        data = GetFile.read_json_file('common/create_strategy_diagram.json')
        data['projectId'] = project_id
        data['parentId'] = response['data'][0]['id']
        data['applyStereotypes'][0] = '16365359598784'
        response = GetHttp().do_post_request(url=url, data=data)
        id_ = response['data'][0]['id']

        # 查询图diagramid
        response = GetHttp().do_get_request(url=f'shape/diagram/allShape?projectId={project_id}&modelId={id_}')

        # 图拖拽服务元素
        data = GetFile.read_json_file('common/create_capability.json')
        data['projectId'] = project_id
        data['targetShapeId'] = response['data'][0]['diagramId']
        data['diagramId'] = data['targetShapeId']
        data['sourceType'] = 'UAF::Services::Taxonomy::Service'
        data['payload']['applyStereotypes'][0] = data['sourceType']
        shape_list = []
        data2 = GetFile.read_json_file('common/shape_connect.json')
        for x in range(100, 500, 200):  # 4000
            for y in range(100, 500, 100):  # 4000
                data['point']['x'] = x
                data['point']['y'] = y
                response = GetHttp().do_post_request(url=APIs.ShapeSiderBarDropAPI, data=data)
                shape_id = response['data']['shapeId']
                shape_ = {"shape_id": shape_id, "x": x, "y": y}
                shape_list.append(shape_)
                if len(shape_list) >= 2:
                    data2['projectId'] = project_id
                    data2['diagramId'] = data['targetShapeId']
                    data2['sourceShapeId'] = shape_list[0]['shape_id']
                    data2['targetShapeId'] = shape_list[1]['shape_id']
                    data2['waypoint'][0]['x'] = shape_list[0]['x']
                    data2['waypoint'][0]['y'] = shape_list[0]['y']
                    data2['waypoint'][1]['x'] = shape_list[1]['x']
                    data2['waypoint'][1]['y'] = shape_list[1]['y']
                    response = GetHttp().do_post_request(url=APIs.ShapeConnectShapeApI, data=data2)
                    shape_list.pop(0)
            else:
                pass
        return response

    @GetTime.run_cost_time
    # 根模型下创建人事
    def model_create_personnel(self, project_id, root_id, url=APIs.ModelCreateAPI,
                               json_file='common/create_strategy.json'):
        # 人事
        data = GetFile.read_json_file(json_file)
        data['projectId'] = project_id
        data['parentId'] = root_id
        data['applyStereotypes'][0] = '16360246779520'
        response = GetHttp().do_post_request(url=url, data=data)

        # 人事分类图
        data = GetFile.read_json_file('common/create_strategy_diagram.json')
        data['projectId'] = project_id
        data['parentId'] = response['data'][0]['id']
        data['applyStereotypes'][0] = '16365400512704'
        response = GetHttp().do_post_request(url=url, data=data)
        id_ = response['data'][0]['id']

        # 查询图diagramid
        response = GetHttp().do_get_request(url=f'shape/diagram/allShape?projectId={project_id}&modelId={id_}')

        data = GetFile.read_json_file('common/create_capability.json')
        data['projectId'] = project_id
        data['targetShapeId'] = response['data'][0]['diagramId']
        data['diagramId'] = data['targetShapeId']
        data['sourceType'] = 'Organization'
        data['payload']['applyStereotypes'][0] = 'UAF::Personnel::Taxonomy::Organization'
        shape_list = []
        data2 = GetFile.read_json_file('common/shape_connect.json')
        for x in range(100, 500, 200):  # 4000
            for y in range(100, 500, 100):  # 4000
                data['point']['x'] = x
                data['point']['y'] = y
                response = GetHttp().do_post_request(url=APIs.ShapeSiderBarDropAPI, data=data)
                shape_id = response['data']['shapeId']
                shape_ = {"shape_id": shape_id, "x": x, "y": y}
                shape_list.append(shape_)
                if len(shape_list) >= 2:
                    data2['projectId'] = project_id
                    data2['diagramId'] = data['targetShapeId']
                    data2['sourceShapeId'] = shape_list[0]['shape_id']
                    data2['targetShapeId'] = shape_list[1]['shape_id']
                    data2['waypoint'][0]['x'] = shape_list[0]['x']
                    data2['waypoint'][0]['y'] = shape_list[0]['y']
                    data2['waypoint'][1]['x'] = shape_list[1]['x']
                    data2['waypoint'][1]['y'] = shape_list[1]['y']
                    response = GetHttp().do_post_request(url=APIs.ShapeConnectShapeApI, data=data2)
                    shape_list.pop(0)
            else:
                pass
        return response

    @GetTime.run_cost_time
    # 根模型下创建资源
    def model_create_resources(self, project_id, root_id, url=APIs.ModelCreateAPI,
                               json_file='common/create_strategy.json'):
        # 资源
        data = GetFile.read_json_file(json_file)
        data['projectId'] = project_id
        data['parentId'] = root_id
        data['applyStereotypes'][0] = '16360276425152'
        response = GetHttp().do_post_request(url=url, data=data)

        # 资源分类图
        data = GetFile.read_json_file('common/create_strategy_diagram.json')
        data['projectId'] = project_id
        data['parentId'] = response['data'][0]['id']
        data['applyStereotypes'][0] = '16365469399616'
        response = GetHttp().do_post_request(url=url, data=data)
        id_ = response['data'][0]['id']

        # 查询图diagramid
        response = GetHttp().do_get_request(url=f'shape/diagram/allShape?projectId={project_id}&modelId={id_}')

        data = GetFile.read_json_file('common/create_capability.json')
        data['projectId'] = project_id
        data['targetShapeId'] = response['data'][0]['diagramId']
        data['diagramId'] = data['targetShapeId']
        data['sourceType'] = 'ResourceArchitecture'
        data['payload']['applyStereotypes'][0] = 'UAF::Resources::Taxonomy::ResourceArchitecture'
        shape_list = []
        data2 = GetFile.read_json_file('common/shape_connect.json')
        for x in range(100, 500, 200):  # 4000
            for y in range(100, 500, 100):  # 4000
                data['point']['x'] = x
                data['point']['y'] = y
                response = GetHttp().do_post_request(url=APIs.ShapeSiderBarDropAPI, data=data)
                shape_id = response['data']['shapeId']
                shape_ = {"shape_id": shape_id, "x": x, "y": y}
                shape_list.append(shape_)
                if len(shape_list) >= 2:
                    data2['projectId'] = project_id
                    data2['diagramId'] = data['targetShapeId']
                    data2['sourceShapeId'] = shape_list[0]['shape_id']
                    data2['targetShapeId'] = shape_list[1]['shape_id']
                    data2['waypoint'][0]['x'] = shape_list[0]['x']
                    data2['waypoint'][0]['y'] = shape_list[0]['y']
                    data2['waypoint'][1]['x'] = shape_list[1]['x']
                    data2['waypoint'][1]['y'] = shape_list[1]['y']
                    response = GetHttp().do_post_request(url=APIs.ShapeConnectShapeApI, data=data2)
                    shape_list.pop(0)
            else:
                pass
        return response


if __name__ == "__main__":
    print(os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0])))+os.sep+'data'+os.sep+'auto_can_del')
